/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package quce_la

import (
	"strconv"
	"errors"
)

func weightToInt(weight interface{}) (num int) {
	switch w := weight.(type) {
	case string:
		if w == "" {
			return 0
		} else {
			i, err := strconv.Atoi(w)
			if err != nil {
				return 0
			}
			return i
		}
	case float64:
		return int(w)
	case int:
		return w
	default:
		panic(errors.New("unsupported weight type"))
	}
}

func getAnswerList(questions [][]int) (list []int) {
	if len(questions) > 1 {
		next := questions[1:]
		list = getAnswerList(next)
		var lists = make(map[int]struct{}, 20)
		for _, answer := range questions[0] {
			for _, l := range list {
				lists[answer+l] = struct{}{}
			}
		}
		list = make([]int, len(lists))
		i := 0
		for key := range lists {
			list[i] = key
			i++
		}
		return list
	} else if len(questions) == 1 {
		return questions[0]
	} else {
		return nil
	}
}
